var start = 0;
function getUpdates(id) {

	$.getJSON('/UBCDatingServer/inbox?userid=' + id+"&start="+start, msgCallback);
}
function delMsg(id) {
	$.getJSON('/UBCDatingServer/delmsg?toDel=' + id);
}
function msgCallback(data){

	for(var i=0; i<data.messages.length;i++)
	{
		var message = data.messages[i];
		var messageNum = $("#msg_"+message.msgNum);
		if(messageNum[0] === undefined)
			{
				$("#MsgTable").hide().append("<tr><td id='msg_"+message.msgNum+"'>"+message.from+"</td><td>"+message.msg+"</td><td>"+message.time+"</td><td ><img class='view' id="+message.msgNum+" src='images/email.png' alt='View' /></td><td><img class='delete' id="+message.msgNum+" src='images/delete.png' alt='Angry face' /> </td></tr>").fadeIn('slow');
			
			
			}
	}
	$('.delete').click(function(){
		var ans = confirm("Are you sure you want to delete this message?");
		if(ans){
			delMsg(this.id);
		$(this).delay(300).queue(function() {
			$(this).parent().parent().fadeOut("slow");
			});
	}
	    
	});
	$('.view').click(function(){
		window.location = "viewMessage.jsp?msgID="+this.id;
	    
	});
	
	
	
}

function init(){
	window.userID = $("#myID").text();
	getUpdates(window.userID);
	window.setInterval(getUpdates, 10000);
	
}
window.onload = init;
